<?php


namespace app\common\model;

use think\facade\Db;
use think\facade\Session;

use app\common\model\Common as CommonModel;

class Fields extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);

        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

//        if(isset($post['title']) && !empty($post['title'])){
//            $where[] = ['title','like','%'.$post['title'].'%'];
//        }

        if(isset($post['name']) && !empty($post['name'])){
            $where[] = ['name','=',$post['name']];
        }

        if(isset($post['field']) && !empty($post['field'])){
            $where[] = ['field','=',$post['field']];
        }

        if(isset($post['moduleid']) && !empty($post['moduleid'])){
            $where[] = ['moduleid','=',$post['moduleid']];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = ['orderid'=>'asc',$pk => 'asc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        //$sysfield = config("app.sysfield");
        $fieldType = config("app.fieldType");
        foreach($list as $k => $v){
            $list[$k]['add_time'] = date("Y-m-d",$v['add_time']);

            $list[$k]['issystem_name'] = $v['issystem'] == 1?'是':'否';
            $list[$k]['required_name'] = $v['required'] == 1?'是':'否';
            $list[$k]['type_name'] = $fieldType[$v['type']]."[".$v['type']."]";

        }
        return $list;
    }

    /**
     * $moduleid 模型id
     * $fields 字符字符串
     */
    public function fieldsArr($moduleid,$fields){

        $arr = [
            'catid'=>[
                'field'=>'catid',
                'name'=>'栏目',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'smallint',
                'maxlength'=>5,
                'pattern'=>'',
                'errormsg'=>'必须选择一个栏目',
                'class'=>'',
                'type'=>'catid',
                'setup'=>'',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>0,
            ],

            'userid'=>[
                'field'=>'userid',
                'name'=>'操作用户id',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'number',
//                'setup'=>"array ('numbertype' =>'1','decimaldigits' =>'0','default' =>'',)",
                'setup'=>'{"numbertype":"1","decimaldigits":"0","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>2,
                'is_form'=>2,
            ],

            'title'=>[
                'field'=>'title',
                'name'=>'标题',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'标题必须为1-80个字符',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],
            'picurl'=>[
                'field'=>'picurl',
                'name'=>'缩略图',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>255,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'image',
//                'setup'=>"array ('upload_allowext' => 'jpg|jpeg|gif|png',)",
                //'setup'=>'{"upload_allowext":"'.config('webconfig')['cfg_upload_img_type'].'"}',
                'setup'=>'{"upload_allowext":"'.config('webconfig')['cfg_upload_img_type'].'","upload_size":"","is_crop":"2","upload_size_width":"","upload_size_height":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],

            'keywords'=>[
                'field'=>'keywords',
                'name'=>'关键词',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>255,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'textarea',
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'description'=>[
                'field'=>'description',
                'name'=>'SEO简介',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'text',
                'maxlength'=>0,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'textarea',
//                'setup'=>"array ('fieldtype' => 'mediumtext','rows' => '4','cols' => '55','default' => '',)",
                'setup'=>'{"fieldtype":"text","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,

            ],

            'content'=>[
                'field'=>'content',
                'name'=>'详细内容',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'text',
                'maxlength'=>0,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'editor',
                //'setup'=>'{"edittype":"UEditor"}',
                'setup'=>'{"edittype":"UEditor","fieldtype":"text"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'orderid'=>[
                'field'=>'orderid',
                'name'=>'排列排序',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'number',
//                'setup'=>"array ('numbertype' =>'1','decimaldigits' =>'0','default' =>'',)",
                'setup'=>'{"numbertype":"1","decimaldigits":"0","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>1,
            ],

            'hits'=>[
                'field'=>'hits',
                'name'=>'点击次数',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'number',
//                'setup'=>"array ('size' =>'10','numbertype' =>'1','decimaldigits' =>'0','default' =>'0',)",
                'setup'=>'{"numbertype":"1","decimaldigits":"0","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,

            ],

            'add_time'=>[
                'field'=>'add_time',
                'name'=>'添加时间',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'datetime',
                'setup'=>'{"datetype":"timestamp"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>1,
                'is_form'=>2,
            ],
            'update_time'=>[
                'field'=>'update_time',
                'name'=>'更新时间',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'datetime',
                'setup'=>'{"datetype":"timestamp"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>0,
                'is_form'=>2,
            ],

            'status'=>[
                'field'=>'status',
                'name'=>'状态',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'radio',
//                'setup'=>"array ('options' =>'开启|1,关闭|0','fieldtype' =>'tinyint','numbertype' =>'1','labelwidth' =>'75','default' =>'1',)",
                'setup'=>'{"options":"\u5f00\u542f|1,\u5173\u95ed|2","fieldtype":"int","numbertype":"1","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],
            'source'=>[
                'field'=>'source',
                'name'=>'文章来源',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>50,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
                //'setup'=>"array ('default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],

            'author'=>[
                'field'=>'author',
                'name'=>'作者编辑',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>50,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ('default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],

            'linkurl'=>[
                'field'=>'linkurl',
                'name'=>'跳转链接',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>255,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ('default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>0,
            ],

            'picarr'=>[
                'field'=>'picarr',
                'name'=>'组图',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'text',
                'maxlength'=>0,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'images',
//                'setup'=>"array ('upload_allowext' => 'jpg|jpeg|gif|png', )",
                //'setup'=>'{"upload_allowext":"'.config('webconfig')['cfg_upload_img_type'].'"}',
                'setup'=>'{"upload_allowext":"'.config('webconfig')['cfg_upload_img_type'].'","upload_size":"","is_crop":"2","upload_size_width":"","upload_size_height":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'video'=>[
                'field'=>'video',
                'name'=>'视频上传',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>255,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'image',
                'setup'=>'{"upload_allowext":"'.config('webconfig')['cfg_upload_media_type'].'"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'name'=>[
                'field'=>'name',
                'name'=>'姓名',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],
            'mobile'=>[
                'field'=>'mobile',
                'name'=>'手机号码',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],
            'password'=>[
                'field'=>'password',
                'name'=>'密码',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"1","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'type'=>[
                'field'=>'type',
                'name'=>'类型',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],
            'code'=>[
                'field'=>'code',
                'name'=>'验证码',
                'tips'=>'',
                'required'=>1,
                'minlength'=>0,
                'fieldtype'=>'varchar',
                'maxlength'=>150,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'title',
//                'setup'=>"array ( 'default' => '','ispassword' => '0','fieldtype' => 'varchar',)",
                'setup'=>'{"default":"","ispassword":"0","fieldtype":"varchar"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>0,
            ],

            'expire_time'=>[
                'field'=>'expire_time',
                'name'=>'过期时间',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'datetime',
                'setup'=>'{"datetype":"timestamp"}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>1,
                'add_time'=>time(),
                'is_table'=>1,
                'is_form'=>2,
            ],

            'is_open'=>[
                'field'=>'is_open',
                'name'=>'是否开启',
                'tips'=>'',
                'required'=>0,
                'minlength'=>0,
                'fieldtype'=>'int',
                'maxlength'=>11,
                'pattern'=>'',
                'errormsg'=>'',
                'class'=>'',
                'type'=>'radio',
//                'setup'=>"array ('options' =>'开启|1,关闭|0','fieldtype' =>'tinyint','numbertype' =>'1','labelwidth' =>'75','default' =>'1',)",
                'setup'=>'{"options":"\u5f00\u542f|1,\u5173\u95ed|2","fieldtype":"int","numbertype":"1","default":""}',
                'ispost'=>1,
                'unpostgroup'=>'',
                //'orderid'=>GetOrderID('fields'),
                'status'=>1,
                'issystem'=>0,
                'add_time'=>time(),
                'is_table'=>1,
            ],

        ];

        $fields_arr = explode(',',$fields);
        $new_fields = [];
        foreach ($fields_arr as $k=>$v){
            $new_fields[$k] = $arr[$v];
        }
        foreach($new_fields as $k=>$v){
            $new_fields[$k]['moduleid'] = $moduleid;
        }
        return $new_fields;
    }



    //遍历指定模型的字段
    public function getModuleFields($moduleid){
        $fields_res = DB::name("fields")->where([
            "moduleid"=>$moduleid,
            "status" => 1,
            "is_table" => 1
        ])
            ->order("orderid asc")
            ->select()->toArray();
        foreach($fields_res as $k=>$v){
            if($v['setup']){
                $fields_res[$k]['setup'] = json_decode($v['setup'],true);
            }

            //用来切开 options 字段
            if($v['type'] == 'radio' or $v['type'] == 'checkbox'){
                //eval("\$arr = ".$v['setup'].'; ');
                $arr = json_decode($v['setup'],true);
                $fields_res[$k]['setup'] = $arr;
                $fields_res[$k]['setup']['options_name'] = $fields_res[$k]['setup']['options'];
                $options_arr = explode(',', $fields_res[$k]['setup']['options']);
                $fields_res[$k]['setup']['options']= $options_arr;

                foreach($fields_res[$k]['setup']['options'] as $k2=>$v2){
                    $optin_arr = explode('|', $v2);
                    $fields_res[$k]['setup']['options'][$k2] = $optin_arr;
                    $fields_res[$k]['setup']['options_two'][$optin_arr[1]] = $optin_arr[0];
                }
            }

        }
        return $fields_res;
    }

    //获取字段 type=1 表格显示，type=2 表单显示
    public function getFieldsData($moduleid,$type){
        $map = [];
        if($type == 1){
            $map['is_table'] = ['=',1];
        }else{
            $map['is_form'] = ['=',1];
        }
        $arr_field = DB::name('fields')
            ->where([
                'moduleid'=>$moduleid,
                'status'=>1,
            ])
            ->where($map)
            ->order('orderid asc')->select()->toArray();
        return $arr_field;
    }


}